﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Data;
using System.Collections;

namespace TaoBaoProject.Common
{
  public class Utility
  {
    //Note: RegisterDAL里面有这个方法 日后需要把它给弄掉！~~
    public static bool CheckAllAreNumbers(string str)
    {
      bool isFlag = true;
      foreach (char item in str)
      {
        if (item == ',')
          continue;
        if (!(item >= 48 && item <= 58))
        {
          isFlag = false;
        }
      }
      return isFlag;
    }

    public static string GetJsonString(DataTable dt)
    {
      string res = "";
      IList<Hashtable> mList = new List<Hashtable>();
      try
      {
        foreach (DataRow row in dt.Rows)
        {
          Hashtable ht = new Hashtable();
          foreach (DataColumn col in dt.Columns)
          {
            ht.Add(col.ColumnName, row[col.ColumnName]);
          }
          mList.Add(ht);
        }

        //使用了NewtonSoft.Json库中的方法.
        res = Newtonsoft.Json.JsonConvert.SerializeObject(mList);
      }
      catch (Exception ee)
      {
        string error = ee.Message;
      }

      return res;
    }

    public static void SaveAllFiles(HttpFileCollection imgs, ref string carFront, ref string carBack, ref string ph)
    {
      try
      {
        if (imgs.Count > 0)
        {
          for (int i = 0; i < imgs.Count; i++)
          {
            if (imgs[i].ContentLength != 0)
            {
              string oldName = imgs[i].FileName.ToString();
              string imgName = Guid.NewGuid().ToString();
              string imgType = oldName.ToString().Substring(oldName.LastIndexOf("."));
              string imgPath = "~/UploadImgs/" + imgName + imgType;
              imgs[i].SaveAs(HttpContext.Current.Server.MapPath(imgPath));
              if (HttpContext.Current.Request.Files["IdentityCardFront"].FileName == imgs[i].FileName)
                carFront = imgName + imgType;
              if (HttpContext.Current.Request.Files["IdentityCardBack"].FileName == imgs[i].FileName)
                carBack = imgName + imgType;
              if (HttpContext.Current.Request.Files["Photo"].FileName == imgs[i].FileName)
                ph = imgName + imgType;
            }
          }
        }
      }
      catch (Exception ex)
      {

      }
    }

    public static string Path()
    {
      return HttpContext.Current.Server.MapPath("./");
    }
  }
}